﻿
/*
 * @Author: wss 
 * @Date: 2020-10-21 15:54:58 
 * @Last Modified by: wss
 * @Last Modified time: 2020-10-21 16:08:46
 */
using System;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UIAmountLerpBar - 血条组件，血条会根据数值变化，有不同的层级
/// ver 1.0 -  基本组件功能 - 2020-10-21 15:57:59 
/// </summary>
public class UIAmountLerpBar : MonoBehaviour
{
    [Header("===== 图片 =====")]
    public Image mBgBar;
    public Image mColorBar1;
    public Image mColorBar2;

    [Header("===== COLOR 颜色 =====")]
    public Color damageColor;
    public Color healColor;
    public Color baseColor;

    public bool startFillAnimate = false;
    [Range(0, 1)]
    public float progress = 1.0f;
    private float _progress = 0;
    public float lerpSpeed = 2f;

    private void Awake()
    {
        if (!startFillAnimate)
        {
            ForceSetProgress(1);
        }
    }

    private void OnValidate()
    {
        mColorBar2.color = baseColor;
        ForceSetProgress(this.progress);
    }

    public void ForceSetProgress(float value)
    {
        _progress = progress = value;
        this.mColorBar2.fillAmount = _progress;
        this.mColorBar1.fillAmount = progress;
    }
    // Update is called once per frame
    void Update()
    {
        _progress = Mathf.Lerp(_progress, progress, lerpSpeed * Time.deltaTime);

        if (_progress < progress)
        {
            mColorBar1.color = healColor;
            mColorBar2.color = baseColor;
            this.mColorBar2.fillAmount = _progress;
            this.mColorBar1.fillAmount = progress;
        }
        else
        {
            mColorBar1.color = damageColor;
            mColorBar2.color = baseColor;
            this.mColorBar1.fillAmount = _progress;
            this.mColorBar2.fillAmount = progress;

        }


    }
}
